package com.bitbakery.plugin.cl.lang;

import com.bitbakery.plugin.cl.lang.lexer.CLLexer;
import com.bitbakery.plugin.cl.lang.lexer.CLTokenTypes;
import com.bitbakery.plugin.cl.lang.psi.CLDefmacro;
import com.bitbakery.plugin.cl.lang.psi.CLDefun;
import com.intellij.lang.cacheBuilder.DefaultWordsScanner;
import com.intellij.lang.cacheBuilder.WordsScanner;
import com.intellij.lang.findUsages.FindUsagesProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: Kurt
 * Date: Feb 21, 2007
 * Time: 7:56:06 PM
 * To change this template use File | Settings | File Templates.
 */
class CLFindUsagesProvider implements FindUsagesProvider {
    public boolean canFindUsagesFor(PsiElement psiElement) {
        return psiElement instanceof PsiNamedElement;
    }

    public String getHelpId(PsiElement psiElement) {
        return null;
    }

    @NotNull
    public String getType(PsiElement element) {
        if (element instanceof CLDefmacro) {
            return "defmacro";
        }
        if (element instanceof CLDefun) {
            return "defun";
        }
        return "";
    }

    @NotNull
    public String getDescriptiveName(PsiElement element) {
        String name = ((PsiNamedElement) element).getName();
        return name != null ? name : "";
    }

    @NotNull
    public String getNodeText(PsiElement element, boolean useFullName) {
        return getDescriptiveName(element);
    }

    public WordsScanner getWordsScanner() {
        return new CommonLispWordsScanner();
    }

    private static class CommonLispWordsScanner extends DefaultWordsScanner {
        public CommonLispWordsScanner() {
            super(new CLLexer(),
                    TokenSet.create(CLTokenTypes.DEFUN, CLTokenTypes.DEFMACRO),
                    CLTokenTypes.COMMENTS,
                    CLTokenTypes.LITERALS);
        }
    }
}
